package com.sugon.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import com.sugon.annotation.DataFilter;
import com.sugon.dao.SysDeptDao;
import com.sugon.entity.SysDeptEntity;
import com.sugon.modules.sys.service.ISysDeptManager;
import com.sugon.service.SysDeptService;
import com.sugon.utils.Constant;
import com.sugon.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Optional;


/**
 * 部门管理
 *
 * @author liepngjun
 * @date 2017年11月18日 下午13:13:23
 */
@RestController
@RequestMapping("/sys/dept")
public class SysDeptController extends AbstractController {
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private ISysDeptManager deptManager;
    @Autowired
    private SysDeptDao deptDao;

    /**
     * 加载铸造车间列表
     * 目前默认根据部门DCODE的前两位为ZZ的默认为铸造车间
     */
    @RequestMapping("/loadFoundryWorkshopList")
    public R loadFoundryWorkshopList() {
        return R.ok().put("foundryWorkshopList", sysDeptService.getZhuZCJ());
    }

    /**
     * 获取指定部门level的部门列表
     *
     * @return
     */
    @RequestMapping("/listByDeptLevel")
    public R listByDeptLevel(@RequestParam Integer deptLevel) {
        List<SysDeptEntity> deptList = deptDao.listByDeptLevel(deptLevel);
        return R.ok().put("list", Optional.ofNullable(deptList).orElseGet(() -> CollectionUtil.newArrayList()));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sys:dept:list")
    @DataFilter(deptAlias = "DEPT_ID", userAlias = "CREATE_USER")
    public R list(@RequestParam Map<String, Object> params) {
        List<SysDeptEntity> deptList = sysDeptService.queryList(params);
        // 为了呈现树状
        if (CollectionUtil.isNotEmpty(deptList)) {
            deptList.get(0).setParentId(null);
        }
        return R.ok().put("list", deptList);
    }

    /**
     * 选择部门(添加、修改菜单)
     */
    @RequestMapping("/select")
    @DataFilter(deptAlias = "DEPT_ID", userAlias = "CREATE_USER")
    @RequiresPermissions("sys:dept:select")
    public R select(@RequestParam Map<String, Object> params) {
        //如果不是超级管理员，则只能查询本部门及子部门数据
        List<SysDeptEntity> deptList = sysDeptService.queryList(params);
        return R.ok().put("deptList", deptList);
    }

    /**
     * 获取铸造车间列表
     */
    @RequestMapping("/getCJ")
    public R getCJ() {
        //获取车间
        List<SysDeptEntity> deptList = sysDeptService.getZhuZCJ();
        return R.ok().put("deptList", deptList);
    }

    /**
     * 上级部门Id(管理员则为0)
     */
    @RequestMapping("/info")
//    @RequiresPermissions("sys:dept:list")
    public R info() {
        String deptId = "0";
        if (!Constant.SUPER_ADMIN.equals(getUserId())) {
            SysDeptEntity dept = sysDeptService.queryObject(getDeptId());
            deptId = dept.getParentId();
        }

        return R.ok().put("deptId", deptId);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{deptId}")
    @RequiresPermissions("sys:dept:info")
    public R info(@PathVariable("deptId") String deptId) {
        SysDeptEntity dept = sysDeptService.queryObject(deptId);

        return R.ok().put("dept", dept);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:dept:save")
    public R save(@RequestBody SysDeptEntity dept) {
        return sysDeptService.save(dept) > 0 ? R.ok() : R.error();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:dept:update")
    public R update(@RequestBody SysDeptEntity dept) {
        sysDeptService.cascadeUpdate(dept);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:dept:delete")
    public R delete(String deptId) {
        //判断是否有子部门
        List<String> deptList = sysDeptService.queryDetpIdList(deptId);
        if (deptList.size() > 0) {
            return R.error("请先删除子部门");
        }
        return sysDeptService.delete(deptId) > 0 ? R.ok() : R.error();
    }

    @RequestMapping("/getDeptType/{deptId}")
    public R getDeptTypeById(@PathVariable("deptId") String deptId) {
        SysDeptEntity entity = sysDeptService.queryObject(deptId);
        return R.ok().put("deptType", entity.getDeptType());
    }

    @RequestMapping("/getKey")
    public R getKey(String type) {
//        String type = map.get("type");
        List list = sysDeptService.getKey(type);
        return R.ok().put("typeList", list);
    }

    @RequestMapping("/validCjCode")
    public R validCjCode(@RequestParam String cjCode) {
        Map<String, Object> param = MapUtil.newHashMap();
        param.put("dcode", cjCode);
        param.put("deptLevel", 2);
        return CollectionUtil.isNotEmpty(deptDao.queryList(param)) ? R.ok() : R.error("请输入正确的车间代码！");
    }

    @GetMapping("/loadDeviceOrganization")
    public R loadDeviceOrganization(@RequestParam(required = false, defaultValue = "3") Integer deptLevel) {
        return R.ok().put("deviceOrganizationList", deptManager.loadDeviceOrganization(deptLevel));
    }

}
